<template>
  <div>
    <el-drawer v-model="drawer" title="新增联系人" :with-header="true">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="姓名：" prop="contactName">
          <el-input v-model="ruleForm.contactName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="职务：" prop="title">
          <el-input v-model="ruleForm.title" placeholder="请输入职务" />
        </el-form-item>
        <el-form-item label="电话：" prop="phone">
          <el-input v-model="ruleForm.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="地址：" prop="address">
          <el-input v-model="ruleForm.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input v-model="ruleForm.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="drawer = false">取消</el-button>
          <el-button type="primary" @click="submit(ruleFormRef)">确定</el-button>
        </span>
      </template>
    </el-drawer>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive, nextTick } from 'vue'
  import { ElMessage } from 'element-plus'
  import type { FormInstance } from 'element-plus'
  import { postContact } from '@/api/controller/project-contact-controller/index'
  const drawer = ref(false)
  const show = (val) => {
    ruleForm.value = {
      projectNumber: val,
      contactName: null,
      title: null,
      phone: null,
      address: null,
      remark: null,
    }
    drawer.value = true
    nextTick(() => {
      ruleFormRef.value.clearValidate()
    })
  }
  const ruleFormRef = ref<FormInstance>()
  const ruleForm = ref({
    projectNumber: null, // 项目编号【必】
    contactName: null, // 联系人名字【必】
    title: null, // 联系人职称
    phone: null, // 联系人电话【必】
    address: null, // 联系人地址
    remark: null, // 联系人备注
  })

  const rules = reactive({
    contactName: [
      { required: true, message: '请输入姓名', trigger: 'blur' },
      { min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' },
    ],
    phone: [
      { required: true, message: '请输入电话', trigger: 'blur' },
      {
        pattern: /^(1[3456789]\d{9}|0\d{2,3}-\d{7,8})$/,
        message: '请输入正确的电话格式',
        trigger: 'blur',
      },
    ],
    title: [{ min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' }],
    address: [{ min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' }],
    remark: [{ min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' }],
  })
  //提交数据
  const submit = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
      if (valid) {
        const result = await postContact(ruleForm.value, { loading: true })
        if (result.code === 200) {
          ElMessage.success('添加成功')
          emit('getData')
          drawer.value = false
        }
      } else {
        ElMessage.warning('请正确填写表单')
        return false
      }
    })
  }
  const emit = defineEmits(['getData'])
  defineExpose({
    show,
  })
</script>

<style scoped lang="scss"></style>
